<?php
/**
 * Created by PhpStorm.
 * User: lijie
 * Date: 2018/10/30
 * Time: 15:30
 */
namespace app\reserve\validate;

use think\Validate;
use think\Db;

class BankcardValidate extends Validate{

    protected $rule = [
        'mix_id'          => ['require',],
        'number' => ['require','regex'=>'/([\d]{4})([\d]{4})([\d]{4})([\d]{4})([\d]{0,})?/','checknumberUnique'],
        'openbank'=>['require'],
        'name' => ['require'],
        'phone'=> ['require'],
        'pagenum'=>['require','number'],
        'type' => ['require','number'],
        'id'=>['require','number'],
    ];

    protected $message = [
        'mix_id'          => 'mix_id不能为空',
        'number'=> '银行卡错误',
        'number.checknumberUnique'=>'银行卡已被绑定',
        'openbank'=> '开户行不能为空',
        'name'=> '联系人不能为空',
        'phone'=> '联系电话不能为空',
        'pagenum'=> '页数不能为空',
        'id'=>'id 不能为空',
    ];

    protected $scene = [
        // 添加银行卡
        'addBankcard' =>['number','openbank','name','phone','mix_id','type'],
        // 银行卡列表
        'selectBankcard' =>['pagenum'],
        // 修改银行卡信息
        'eidtBankcard'=>['id','mix_id','openbank','name','phone','type'],
        // 删除银行卡信息
        'deleteBankcard'=>['id'],
    ];

    protected function checknumberUnique($value,$rule,$data){
        $where = [
            'type' => ['eq',$data['type']],
            'number' => ['eq',$value],
            'status'=>['NEQ',9],
        ];
        $result = Db::name('bankcard')
            ->where($where)
            ->value('number');
        return $result ? false : true;
    }
}